class OriginJScrash(object):

    def __init__(self, perFileData):
        '''
        :param perFileData: origin data of a Jscrash file
        '''
        self.contents = perFileData
        self.path = perFileData[0]
        self.content = perFileData[1]
        self.module_name = self.getModuleName()[0]
        self.module_status = self.getModuleName()[1]
        self.reason = self.getReason()
        self.stacktrace = self.getStacktrace()
        self.crucial = self.is_crucial()

    def getModuleName(self):
        '''
        comment: 获取模块名
        :return:
        '''
        for idx in range(len(self.content)):
            curline: str = self.content[idx]
            if curline.startswith('Module name'):
                if curline.strip() in ['Module name', 'Module name:']:
                    nextline: str = self.content[idx + 1]
                    if nextline.startswith('Pid'):
                        return curline, 0
                    else:
                        return nextline, 1
                else:
                    return curline.split(':')[-1], 1
            else:
                pass
        return '', 0

    def getReason(self):
        for line in self.content:
            curline: str = line
            if curline.startswith('Reason:'):
                return curline
        return None

    def getStacktrace(self):
        for line in self.content:
            curline: str = line.strip()
            if curline.startswith('at'):
                at_func = curline.split('(')[0].strip()
                if at_func == 'at':
                    return ''
                else:
                    if ' ' in at_func:
                        func = at_func.split()[-1]
                    else:
                        func = at_func
                return func
        return ''


    def is_crucial(self):
        crucial = ["com.ohos.mms", "com.ohos.photos", "com.ohos.note", "com.example.kikakeyboard",
                   "com.ohos.callui", "com.ohos.camera", "com.ohos.contacts", "com.ohos.settings",
                   "com.ohos.systemui", "com.ohos.screenlock", "com.ohos.launcher"]
        if self.module_name.strip() in crucial:
            return 1
        return 0